# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")

package(
    default_visibility = ["//visibility:public"],
    features = ["-ctad_warnings"],
)

licenses(["notice"])

# Libraries

cc_library(
    name = "measurements",
    testonly = True,
    srcs = [
        "measurements.cc",
    ],
    hdrs = [
        "public/pw_allocator/benchmarks/measurements.h",
    ],
    features = ["-conversion_warnings"],
    strip_include_prefix = "public",
    deps = [
        "//pw_chrono:system_clock",
        "//pw_containers:intrusive_map",
        "//pw_metric:metric",
    ],
)

cc_library(
    name = "benchmark",
    testonly = True,
    srcs = [
        "benchmark.cc",
    ],
    hdrs = [
        "public/pw_allocator/benchmarks/benchmark.h",
        "public/pw_allocator/benchmarks/config.h",
    ],
    features = ["-conversion_warnings"],
    implementation_deps = ["//pw_assert:check"],
    strip_include_prefix = "public",
    target_compatible_with = select({
        "@platforms//os:linux": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    deps = [
        ":measurements",
        "//pw_allocator",
        "//pw_allocator:block_allocator",
        "//pw_allocator:fragmentation",
        "//pw_allocator:test_harness",
        "//pw_chrono:system_clock",
        "//pw_metric:metric",
        "//pw_tokenizer",
    ],
)

# Binaries

cc_binary(
    name = "best_fit_benchmark",
    testonly = True,
    srcs = [
        "best_fit_benchmark.cc",
    ],
    features = ["-conversion_warnings"],
    deps = [
        ":benchmark",
        "//pw_allocator:best_fit",
        "//pw_random",
    ],
)

cc_binary(
    name = "dual_first_fit_benchmark",
    testonly = True,
    srcs = [
        "dual_first_fit_benchmark.cc",
    ],
    features = ["-conversion_warnings"],
    deps = [
        ":benchmark",
        "//pw_allocator:first_fit",
        "//pw_random",
    ],
)

cc_binary(
    name = "first_fit_benchmark",
    testonly = True,
    srcs = [
        "first_fit_benchmark.cc",
    ],
    features = ["-conversion_warnings"],
    deps = [
        ":benchmark",
        "//pw_allocator:first_fit",
        "//pw_random",
    ],
)

cc_binary(
    name = "last_fit_benchmark",
    testonly = True,
    srcs = [
        "last_fit_benchmark.cc",
    ],
    features = ["-conversion_warnings"],
    deps = [
        ":benchmark",
        "//pw_allocator:first_fit",
        "//pw_random",
    ],
)

cc_binary(
    name = "tlsf_benchmark",
    testonly = True,
    srcs = [
        "tlsf_benchmark.cc",
    ],
    features = ["-conversion_warnings"],
    deps = [
        ":benchmark",
        "//pw_allocator:tlsf_allocator",
        "//pw_random",
    ],
)

cc_binary(
    name = "worst_fit_benchmark",
    testonly = True,
    srcs = [
        "worst_fit_benchmark.cc",
    ],
    features = ["-conversion_warnings"],
    deps = [
        ":benchmark",
        "//pw_allocator:worst_fit",
        "//pw_random",
    ],
)

# Unit tests

pw_cc_test(
    name = "measurements_test",
    srcs = ["measurements_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":measurements",
        "//pw_metric:metric",
    ],
)

pw_cc_test(
    name = "benchmark_test",
    srcs = ["benchmark_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":benchmark",
        ":measurements",
        "//pw_allocator:fragmentation",
        "//pw_allocator:test_harness",
        "//pw_allocator:testing",
        "//pw_random",
    ],
)
